#include "input.h"

Input::Input(const char *file):
	n(0),
	m(0),
	p(0),
	d(0),
	imagem(10,10)
{
	this->readFile(file);
}

Input::~Input(void)
{
	this->pat.clear();
}

bool Input::readFile(const char *file)
{
	fstream read(file,ifstream::in);
	cout<<"Opening "<<file;
	if(read.is_open()){
		string line; line.clear();
		string number; number.clear();
		
		//Pegando as variaveis N M P D
		getline(read, number,' ');
		sscanf(number.c_str(),"%d",&n);	//N dimensao da Matriz
		getline(read, number,' ');
		sscanf(number.c_str(),"%d",&m);	//M dimensao dos quadrantes
		getline(read, number,' ');
		sscanf(number.c_str(),"%d",&p);	//P numero de padroes
		getline(read, number,'\n');
		sscanf(number.c_str(),"%d",&d);	//D numero de celulas/pixels dos padroes
		number.clear();
		cout<<".";
		//Verifica os valores limites das variaveis
		this->verify();
		//Pegando os padroes
		for(register int i = 0; i < p;++i){
			int x = 0,y = 0;
			bool x_minus = false,y_minus = false;
			
			getline(read, line,'\n');
			this->pat.push_back(new Pattern());
			for(register int j = 0; j < (int)line.size();++j){
				char c = line[j];
				
				if(c == '(' || c == ' ' || c == '+'){}
				else if(c == ','){
					sscanf(number.c_str(),"%d",&x);	//armazenando o x
					number.clear();
				}
				else if(c == ')'){
					sscanf(number.c_str(),"%d",&y);	//armazenando o x e y
					number.clear();
					
					if(x_minus){
						if(y_minus)this->pat.back()->addPixel(-x,-y);
						else this->pat.back()->addPixel(-x,y);
						x_minus = false;y_minus = false;
					}
					else{
						if(y_minus)this->pat.back()->addPixel(x,-y);
						else this->pat.back()->addPixel(x,y);
						x_minus = false;y_minus = false;
					}
				}
				else if(c == '-'){
					if(line[j-1] == '(')x_minus = true;
					else if(line[j-1] == ',')y_minus = true;
				}
				else number += c;
				
			}
			cout<<".";
		}
		line.clear();
		number.clear();
		
		//Pegando a Imagem
		imagem.create(n,n);
		int x = 0,y = n-1;
		//int ind = 0;
		for(register int i = 0; i < n;++i){
			
			
			getline(read, line,'\n');
			
			for(register int j = 0; j < (int)line.size();++j){
				char c = line[j];
				int value = 0;
				if(c == ' '){
					sscanf(number.c_str(),"%d",&value);	//armazenando o valor
					number.clear();
					//imagem.getPixel(ind) = value;
					imagem.getPixel(x,y) = value;
					//ind++;
					x++;
				}
				else number += c;
				
				if(j == (int)line.size()-1){
					if(c != ' '){
						sscanf(number.c_str(),"%d",&value);	//armazenando o valor
						number.clear();
						//imagem.getPixel(ind) = value;
						imagem.getPixel(x,y) = value;
						//ind++;
						x++;
					}
				}
				
			}
			line.clear();
			y--; x =0;
		}
		
		cout<<"success"<<endl;
		read.close();
		
		return true;
	}
	else{ cout<<"failed"<<endl;read.close();this->verify();return false;}
}

void Input::print(void)
{
	cout<<"Entrada->Variaveis:"<<endl;
	cout<<'\t'<<"N: "<<n<<endl;
	cout<<'\t'<<"M: "<<m<<endl;
	cout<<'\t'<<"P: "<<p<<endl;
	cout<<'\t'<<"D: "<<d<<endl;
	cout<<"Entrada->Padroes:"<<endl;
	for(register int i = 0; i < (int)pat.size();++i){
		cout<<'\t'<<i<<": ";
		for(register int j = 0; j < pat[i]->size();++j){
			cout<<'('<<pat[i]->getPixelX(j)<<','<<pat[i]->getPixelY(j)<<") ";
		}
		cout<<endl;
	}
	cout<<"Entrada->Imagem:"<<endl;
	imagem.print();
}

void Input::verify(void)
{
	//4<= N <= 1000
	if(n < 4)n = 4;	else if(n > 1000)n=1000;
	//2<= M <= 250
	if(m < 2)n = 2;	else if(n > 250)n=250;
	//M <= N
	if(n < m){
		int temp = n;
		n = m;
		m = temp;
	}
	//1<= P <= 5
	if(p < 1)n = 1;	else if(p > 5)n=5;
	//3<= D <= 10
	if(d < 3)n = 3;	else if(d > 10)n=10;
}
